import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, Validators } from '@angular/forms';
import { ApiService } from '../../../core/api/api.service';
import { MessageService } from '../../../core/service/message.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { I18nService } from 'src/app/core/i18n/i18n.service';
@Component({
	selector: 'app-role-authority',
	templateUrl: './role-authority.component.html',
})
export class RoleAuthorityComponent implements OnInit {
	validateForm;
	activeData: any = {};
	tableLoading = false;
	menuTree = [];
	menuList = [];
	isIndeterminate = false;
	isAllDisplayDataChecked = false;
	role:any = {};
	ngOnInit() {
		this.message.setBreadcrumb({
			breadcrumbs: [{ name: '系统角色管理' }],
		});
		this.validateForm = this.fb.group({
			zh_CN_name: [null, [Validators.required]],
			en_US_name: [null, [Validators.required]],
			ja_JP_name: [null, [Validators.required]],
			memo: [null],
		});
		this.activeData = JSON.parse(sessionStorage.getItem('roleData'));
		const { name, memo } = this.activeData;
		let nameObj = JSON.parse(name);
		const data = { zh_CN_name:nameObj.zh_CN,en_US_name:nameObj.en_US,ja_JP_name:nameObj.ja_JP, memo };
		this.validateForm.setValue(data);
		this.getMenuList();
	}
	changeExpand(e, item) {
		if (!e) {
			item.children.forEach(element => {
				element.expand = e;
			});
		}
	}
	getMenuList() {
		this.tableLoading = true;
		this.apiService
			.post('菜单列表', {})
			.then((res: any) => {
				this.menuList = res.data.list;
				this.apiService.post('指定管理员角色权限集合', { roleId: this.activeData.id }).then((data: any) => {
					this.menuTree = this.getTree(res.data.list);
					this.tableLoading = false;
					this.setSelectedList(data.data.auths);
				});
			})
			.catch(res => {
				this.tableLoading = false;
			});
	}
	setSelectedList(res) {
		const obj = {};
		for (let i = 0; i < this.menuList.length; i++) {
			obj[this.menuList[i].id] = this.menuList[i];
		}
		res.forEach(element => {
			if (obj[element.id]) {
				obj[element.id].checked = true;
			}
		});
	}
	getTree(list) {
		const array = [],
			obj = {};
		list.forEach(element => {
			if (element.parentId === 0) {
				array.push(element);
			}
			element.expand = true;
			obj[element.id] = element;
		});
		list.forEach(element => {
			if (obj[element.parentId]) {
				element.$parent = obj[element.parentId];
				obj[element.parentId].children ? obj[element.parentId].children.push(element) : (obj[element.parentId].children = [element]);
			}
		});
		return array;
	}
	checkAll(e) {
		this.menuList.forEach(item => (item.checked = e));
	}
	submit() {
		const array = [];
		this.menuList.forEach(item => {
			if (item.checked) {
				array.push(item.id);
			}
		});
		this.activeData.pageIds = array;
		this.apiService.post('编辑角色', {...this.activeData,...this.validateForm.value}).then((res: any) => {
			this.nzMessageService.success('编辑成功');
			this.router.navigate(['systemMgmt/roleMgmt']);
		});
	}
	refreshStatus(data) {
		if (data.checked) {
			if (data.children) {
				data.children.map(element => {
					element.checked = true;
					if (element.children) {
						element.children.map(e => (e.checked = true));
					}
				});
			}
			if (data.$parent) {
				data.$parent.checked = true;
				if (data.$parent.$parent) {
					data.$parent.$parent.checked = true;
				}
			}
		} else {
			if (data.children) {
				data.children.map(element => {
					element.checked = false;
					if (element.children) {
						element.children.map(e => (e.checked = false));
					}
				});
			}
			if (data.$parent) {
				const a = data.$parent.children.filter(e => e.checked === true);
				console.log(a);
				if (a.length < 1) {
					data.$parent.checked = false;
				}
				if (data.$parent.$parent) {
					data.$parent.$parent.checked = false;
					const a = data.$parent.$parent.children.filter(e => e.checked === true);
					if (a.length < 1) {
						data.$parent.$parent.checked = false;
					}
				}
			}
		}
	}
	constructor(private i18n:I18nService ,private router: Router, private fb: FormBuilder, private apiService: ApiService, private message: MessageService, private nzMessageService: NzMessageService, private modal: NzModalService) {
		this.i18n.get('system.role').subscribe(res=>{
            this.role = res;
        })
	}
}
